﻿ 
/// <reference path="workflowcontroller.ts" />
/// <reference path="../repository/soc.ts" />
 

module WorkBasket {
    export module WFTree {


        var pvid: number;
        var _scope: any;
        export function SetProcessVersionID(id: number, scope) {
            pvid = id;
            _scope = scope;
        }


        export function addRoot() {
        
            return addnode("start", true);
        

        }
        export function addnode(type, forceroot: boolean= false) {


            var MyType = { "type": type };
            var ref = $('#jstree').jstree(true), sel = ref.get_selected();
            var newnode = null;
            var selected = null;
            if (!forceroot) {

                if (!sel.length) {
                    return false;
                }
                selected = sel[0];

                newnode = ref.create_node(selected, MyType);
            } else {
                newnode = ref.create_node(null, MyType);
                selected = "";
            }



            var SOC = WorkBasket.SOC;
            var step = new WorkBasket.Entities.Step(
                0,
                selected,
                "New Node",
                "",
                0,0,true,
                "",
                0,
                pvid,
                type,
                "");

            var obj: any = SOC.Steps().SaveData(step);

            ref.set_id(newnode, obj.StepID);

            return newnode;
        }


        function renameNode(data) {

            var obj: any;
            obj = WorkBasket.SOC.Steps().GetByKey(data.node.id);


            obj.StepName=data.text;
            //check to be sure you are not getting empty arrays...if so,  null them out
            SOC.Steps().SaveData(obj);

        }

        function DeleteNode(data) {
            var obj: any;
            obj = WorkBasket.SOC.Steps().GetByKey(data.id);
            WorkBasket.SOC.Steps().Delete(obj, obj.StepID);

        }
        function loadproperties(data) {

            var step: any;
            _scope.SelectedStep = WorkBasket.SOC.Steps().GetByKey(data.node.id);
            //var ctlType: any = SOC.Forms().GetByKey(ctl.ClassName.replace(/\./g, '_'), null, "GetControlData");
            //var ctlDefaultProps = JSON.parse(ctlType.Properties);
            //if (ctl != null) {
            //    var obj = JSON.parse(ctl.Properties);


            //    scope.Properties = [];
            //    for (var prop in obj) {
            //        if (obj.hasOwnProperty(prop))
            //            scope.Properties.push({ Name: prop, Value: obj[prop], Default: ctlDefaultProps[prop].split(",") });
            //    }
               
            //}
            _scope.$digest();
            
        }




 
        $(function () {

            //$(document).bind("dnd_stop.vakata", function (e, data) {
            //    alert(data);
            //});

            $('#jstree').jstree({
                "core": {
                    "multiple": false,
                    "animation": 0,
                    "check_callback": true,
                    "themes": {
                        "variant": "large"
                    },
               
                    'data': function (obj, cb)
                    {
                        if (pvid != undefined) {
                            var nodes = WorkBasket.SOC.ProcessVersions().GetByParentKey(pvid, "GetTreeJSON", function (returndata) {
                                return returndata;
                            });
                            cb.call(this, nodes);
                        } else {
                            cb.call(this, []);
                        }
                    },
                    },
                "types": {
                    "step": {
                        "icon": "glyphicon glyphicon-cog",
                        "valid_children": ["step", "action"]
                    },
                    "action": {
                        "icon": "glyphicon glyphicon-flash",
                        "max_children": 0


                    },
                    "start": {
                        "icon": "glyphicon glyphicon-flag",
                        "valid_children": ["step", "stop"]
                    },
                    "stop": {
                        "icon": "glyphicon glyphicon-asterisk",
                        "max_children": 0
                    }
                },
                "contextmenu": {
                    "items": function ($node) {
                        var tree = $("#jstree").jstree(true);
                        return {
                            "Create Step": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Create Child Step",
                                "action": function (obj) {
                                    addnode("step");
                                }
                            },
                            "Create Action": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Create action",
                                "action": function (obj) {
                                    addnode("action");
                                }
                            },
                            "Create Terminal Step": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Create Terminal Step",
                                "action": function (obj) {
                                    addnode("stop");
                                }
                            },
                            "Rename": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Rename",
                                "action": function (obj) {
                                    tree.edit($node);
                                }
                            },
                            "Remove": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Remove",
                                "action": function (obj) {
                                    tree.delete_node($node);
                                    DeleteNode($node);
                                }
                            }

                        };
                    }
                },
                "plugins": ["themes", "dnd", "types", "contextmenu"]
            });

            $('#jstree').on("changed.jstree", function (e, data) {
                if (data.selected.length > 0) {
 
                    loadproperties(data);
                }
            });

            $('#jstree').on("rename_node.jstree", function (e, data) {
                renameNode(data);
            });

        });


    }
}